package cn.wizzer.app.web.modules.controllers.open;

import cn.wizzer.app.user.modules.services.account.UserWithdrawService;
import cn.wizzer.app.user.modules.services.account.UserRechargeService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@IocBean
@At("/open/oneWallet")
public class ApiOneWalletController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserRechargeService userRechargeService;
    @Inject
    @Reference
    private UserWithdrawService withdrawService;

    @At("/recharge/notify")
    @Ok("raw:txt")
    public String rechargeNotify() throws Exception {
        StringBuffer sb = getRespBody();
        log.info("xindovndRechargeNotify:" + sb);
        String result = userRechargeService.oneWalletNotify(sb.toString());
        //response(Mvcs.getResp(), HTTP_STATUS.OK,null,result.getBytes());
        return result;
    }
    @At("/withdraw/notify")
    @Ok("raw:txt")
    public String withdrawNotify() throws Exception {
        StringBuffer sb = getRespBody();
        log.info("xindovndRechargeNotify:" + sb);
        String result = withdrawService.oneWalletNotify(sb.toString());
        //response(Mvcs.getResp(), HTTP_STATUS.OK,null,result.getBytes());
        return result;
    }

    private StringBuffer getRespBody() throws IOException {
        StringBuffer sb = new StringBuffer();
        InputStream is = Mvcs.getReq().getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String s = "";
        while ((s = br.readLine()) != null) {
            sb.append(s);
        }
        return sb;
    }
}
